<template>
  <div class="notice-container">
    <div class="notice-header">
      <h2>体质测试通知管理</h2>
      <el-button type="primary" @click="handleAddNotice">发布新通知</el-button>
    </div>

    <!-- 通知列表 -->
    <el-table :data="noticeList" style="width: 100%">
      <el-table-column prop="noticeTitle" label="通知标题" width="200" />
      <el-table-column prop="noticeContent" label="通知内容" />
      <el-table-column prop="createTime" label="发布时间" width="180" />
      <el-table-column prop="status" label="状态" width="100">
        <template #default="scope">
          <el-tag :type="scope.row.status === 0 ? 'success' : 'info'">
            {{ scope.row.status === 0 ? '已发布' : '未知状态'}}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作" width="150">
        <template #default="scope">
          <el-button type="text" @click="handleEdit(scope.row)">编辑</el-button>
          <el-button type="text" @click="handleDelete(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 在表格下方添加分页组件 -->
    <div class="pagination-container">
      <el-pagination
        v-model:current-page="queryParams.current"
        v-model:page-size="queryParams.pageSize"
        :page-sizes="[10, 20, 50, 100]"
        :total="total"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        layout="total, sizes, prev, pager, next"
      />
    </div>

    <!-- 新增/编辑通知对话框 -->
    <el-dialog
      :title="dialogTitle"
      v-model="dialogVisible"
      width="50%"
    >
      <el-form :model="noticeForm" ref="noticeFormRef" :rules="rules">
        <el-form-item label="通知标题" prop="noticeTitle">
          <el-input v-model="noticeForm.noticeTitle" />
        </el-form-item>
        <el-form-item label="通知内容" prop="noticeContent">
          <el-input type="textarea" v-model="noticeForm.noticeContent" rows="6" />
        </el-form-item>
      </el-form>
      <template #footer>
        <el-button @click="dialogVisible = false">取消</el-button>
        <el-button type="primary" @click="submitForm">确定</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import { ref, reactive, onMounted } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'
import myAxios from '../../../plugins/myAxios'

const noticeList = ref([])
const dialogVisible = ref(false)
const dialogTitle = ref('发布通知')
const noticeFormRef = ref(null)

const noticeForm = reactive({
  noticeTitle: '',
  noticeContent: '',
})

const rules = {
  noticeTitle: [{ required: true, message: '请输入通知标题', trigger: 'blur' }],
  noticeContent: [{ required: true, message: '请输入通知内容', trigger: 'blur' }],
}

// 查询参数
const queryParams = reactive({
  current: 1,
  pageSize: 10,
})

const total = ref(0)

// 获取通知列表
const getNoticeList = async () => {
  try {
    const res = await myAxios.get('/admin/notice/page', {
      params: queryParams
    })
    console.log(res)
    noticeList.value = res.records;
    total.value = res.records.length;
  } catch (error) {
    ElMessage.error('获取通知列表失败：' + error)
  }
}

// 添加通知
const handleAddNotice = () => {
  dialogTitle.value = '发布通知'
  noticeForm.noticeTitle = ''
  noticeForm.noticeContent = ''
  dialogVisible.value = true
}

// 编辑通知
const handleEdit = (row) => {
  dialogTitle.value = '编辑通知'
  noticeForm.noticeTitle = row.noticeTitle
  noticeForm.noticeContent = row.noticeContent
  noticeForm.noticeId = row.noticeId // 添加id字段用于编辑
  dialogVisible.value = true
}

// 删除通知
const handleDelete = (row) => {
  ElMessageBox.confirm('确定要删除该通知吗？', '提示', {
    type: 'warning'
  }).then(async () => {
    try {
      await myAxios.post(`/admin/notice/delete/${row.noticeId}`)
      ElMessage.success('删除成功')
      getNoticeList()
    } catch (error) {
      ElMessage.error('删除失败：' + error)
    }
  })
}

// 提交表单
const submitForm = async () => {
  if (!noticeFormRef.value) return
  await noticeFormRef.value.validate(async (valid) => {
    if (valid) {
      try {
        if (noticeForm.noticeId) {
          // 编辑
          await myAxios.post('/admin/notice/update', noticeForm)
        } else {
          // 新增
          await myAxios.post('/admin/notice/add', noticeForm)
        }
        ElMessage.success('保存成功')
        dialogVisible.value = false
        getNoticeList()
      } catch (error) {
        ElMessage.error('保存失败：' + error)
      }
    }
  })
}

// 处理每页显示数量变化
const handleSizeChange = (val) => {
  queryParams.pageSize = val
  getNoticeList()
}

// 处理页码变化
const handleCurrentChange = (val) => {
  queryParams.current = val
  getNoticeList()
}

// 页面加载时获取通知列表
onMounted(() => {
  getNoticeList()
})
</script>

<style scoped>
.notice-container {
  padding: 20px;
}

.notice-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 20px;
}

.pagination-container {
  margin-top: 20px;
  display: flex;
  justify-content: flex-end;
}
</style>
